// rapidjson_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

class Example {
public:
	std::string string;
	std::map<std::string, std::string> map;
	std::vector<int> vector;
	int a;
};

// 将Example对象转换为JSON字符串
std::string toJson(const Example& example) {
	rapidjson::Document doc;
	doc.SetObject();
	// 添加string成员到JSON对象
	rapidjson::Value strValue;
	strValue.SetString(example.string.c_str(), example.string.length(), doc.GetAllocator());
	doc.AddMember("string", strValue, doc.GetAllocator());

	// 添加map成员到JSON对象
	rapidjson::Value mapValue;
	mapValue.SetObject();
	for (const auto& pair : example.map) {
		rapidjson::Value key(pair.first.c_str(), pair.first.length(), doc.GetAllocator());
		rapidjson::Value value(pair.second.c_str(), pair.second.length(), doc.GetAllocator());
		mapValue.AddMember(key, value, doc.GetAllocator());
	}
	doc.AddMember("map", mapValue, doc.GetAllocator());

	// 添加vector成员到JSON对象
	rapidjson::Value vectorValue;
	vectorValue.SetArray();
	for (const auto& item : example.vector) {
		rapidjson::Value value(item);
		vectorValue.PushBack(value, doc.GetAllocator());
	}
	doc.AddMember("vector", vectorValue, doc.GetAllocator());

	// 将JSON对象转换为字符串
	rapidjson::StringBuffer buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	doc.Accept(writer);
	return buffer.GetString();
}

int main() {
	std::map<std::string, std::string> map_test;

	for (size_t i=0;i<10;i++){
		map_test[std::to_string(i)] = "a";
	}

	Example example;
	example.string = "Hello";
	example.map = map_test;
	//example.map = { {"key1", "value1"}, {"key2", "value2"} };
	example.vector = { 1, 2, 3, 4, 5 };
	std::string jsonStr = toJson(example);
	std::cout << "JSON string: " << jsonStr << std::endl;

	return 0;
}